<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="审核状态" prop="state">
        <el-select style="width: 190px" v-model="queryParams.state" placeholder="请选择审核状态" clearable
                   @keyup.enter="handleQuery">
          <el-option
              v-for="(item, index) in allState"
              :key="index"
              :label="item.label"
              :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="车牌号" prop="carNum">
        <el-input
            v-model="queryParams.carNum"
            placeholder="请输入车牌号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入真实姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="身份证号" prop="identifyNum">
        <el-input
            v-model="queryParams.identifyNum"
            placeholder="请输入身份证号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="最新申请时间" style="width: 308px;">
        <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:driverAudit:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="driverAuditList" @selection-change="handleSelectionChange">
      <el-table-column label="用户昵称" align="center" prop="nickName"/>
      <el-table-column label="真实姓名" align="center" prop="userName"/>
      <el-table-column label="车型选择" align="center" prop="carStyle"/>
      <el-table-column label="身份证号" align="center" prop="identifyNum"/>
      <el-table-column label="车牌号" align="center" prop="carNum"/>
      <el-table-column label="加盟城市" align="center" prop="belongCity"/>
      <el-table-column label="最新申请时间" align="center" prop="latestApplicationTime" width="130">
        <template #default="scope">
          <span>{{ parseTime(scope.row.latestApplicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state">
        <template #default="scope">
          <el-tag v-if="scope.row.state == 0" type="success">待审核</el-tag>
          <el-tag v-if="scope.row.state == 1" type="primary">通过</el-tag>
          <el-tag v-if="scope.row.state == 2" type="danger">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right">-->
<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"-->
<!--                     v-hasPermi="['system:driverAudit:edit']">修改-->
<!--          </el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"-->
<!--                     v-hasPermi="['system:driverAudit:remove']">删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" v-if="scope.row.state == 0" @click="handleUpdate(scope.row),isShow(true)"
                     v-hasPermi="['system:driverAudit:edit']">审批
          </el-button>
          <el-button link type="primary" icon="View" v-if="scope.row.state == 1" @click="handleUpdate(scope.row),isShow(false)"
                     v-hasPermi="['system:driverAudit:view']">查看
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:driverAudit:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改司机认证对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="driverAuditRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户昵称" prop="nickName">
          <el-input disabled v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="userName">
          <el-input disabled v-model="form.userName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="identifyNum">
          <el-input disabled v-model="form.identifyNum" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="车型选择" prop="carStyle">
          <el-input disabled v-model="form.carStyle" placeholder="请输入车型选择" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNum">
          <el-input disabled v-model="form.carNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="加盟城市" prop="belongCity">
          <el-input disabled v-model="form.belongCity" placeholder="请输入加盟城市" />
        </el-form-item>

        <el-form-item label="身份证" prop="">
          <el-image style="width: 100px; height: 80px;" v-if="form.identifyCardPositive"
                    :src="form.identifyCardPositive" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.identifyCardPositive]" preview-teleported hide-on-click-modal>
          </el-image>
          <el-image style="width: 100px; height: 80px;" v-if="form.identifyCardNegative"
                    :src="form.identifyCardNegative" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.identifyCardNegative]" preview-teleported hide-on-click-modal>
          </el-image>
        </el-form-item>
        <el-form-item label="驾驶证" prop="">
          <el-image style="width: 100px; height: 80px;" v-if="form.driverCardPositive"
                    :src="form.driverCardPositive" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.driverCardPositive]" preview-teleported hide-on-click-modal>
          </el-image>
          <el-image style="width: 100px; height: 80px;" v-if="form.driverCardNegative"
                    :src="form.driverCardNegative" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.driverCardNegative]" preview-teleported hide-on-click-modal>
          </el-image>
        </el-form-item>
        <el-form-item label="行驶证" prop="">
          <el-image style="width: 100px; height: 80px;" v-if="form.walkCardPositive"
                    :src="form.walkCardPositive" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.walkCardPositive]" preview-teleported hide-on-click-modal>
          </el-image>
          <el-image style="width: 100px; height: 80px;" v-if="form.walkCardNegative"
                    :src="form.walkCardNegative" fit="contain" title="点击查看大图"
                    :preview-src-list="[form.walkCardNegative]" preview-teleported hide-on-click-modal>
          </el-image>
        </el-form-item>


        <el-form-item label="审核状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" >
          <el-button type="primary" @click="submitForm" v-if="flag===1">确 定</el-button>
          <el-button @click="cancel" v-if="flag===1">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DriverAudit">
import {listDriverAudit, getDriverAudit, delDriverAudit, addDriverAudit, updateDriverAudit} from "@/api/system/driverAudit.js";
import { computed } from 'vue';
const {proxy} = getCurrentInstance();

const driverAuditList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const flag = ref(0);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    identifyNum: null,
    carNum: null,
    latestApplicationTime: null,
    state: null,
  },
  rules: {
    identifyNum: [
      {required: true, message: "身份证号不能为空", trigger: "blur"}
    ],
    carNum: [
      {required: true, message: "车牌号不能为空", trigger: "blur"}
    ],
    belongCity: [
      {required: true, message: "加盟城市不能为空", trigger: "blur"}
    ],
    identifyCard: [
      {required: true, message: "身份证不能为空", trigger: "blur"}
    ],
    driveCard: [
      {required: true, message: "驾驶证不能为空", trigger: "blur"}
    ],
  },
  allState: [
    {
      label: "待审核",
      value: 0,
    },
    {
      label: "通过",
      value: 1,
    },
    {
      label: "驳回",
      value: 2,
    },
  ],
});

const {queryParams, form, rules, allState} = toRefs(data);



/** 查询司机认证列表 */
function getList() {
  loading.value = true;
  listDriverAudit(proxy.addDateRange(queryParams.value,dateRange.value)).then(response => {
    driverAuditList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    authenticId: null,
    driverId: null,
    identifyNum: null,
    carId: null,
    carNum: null,
    belongCity: null,
    latestApplicationTime: null,
    state: null,
    auditTime: null,
    identifyCard: null,
    driveCard: null,
    walkCard: null,
    bookLicense: null,
    transportationLicense: null,
    partyLicense: null,
    retireLicense: null,
    driverLicense: null,
    carOperateLicense: null,
    personCar: null
  };
  proxy.resetForm("driverAuditRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
//是否能继续修改
const isShow = (flagStatus) => {
  if(flagStatus){
    flag.value=1;
  }else{
    flag.value=0;
  }
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.authenticId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加司机认证";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _authenticId = row.authenticId || ids.value
  getDriverAudit(_authenticId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改司机认证";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["driverAuditRef"].validate(valid => {
    if (valid) {
      if (form.value.authenticId != null) {
        updateDriverAudit(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDriverAudit(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _authenticIds = row.authenticId || ids.value;
  proxy.$modal.confirm('是否确认删除司机认证编号为"' + _authenticIds + '"的数据项？').then(function () {
    return delDriverAudit(_authenticIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/driverAudit/export', {
    ...queryParams.value
  }, `driverAudit_${new Date().getTime()}.xlsx`)
}

getList();
</script>
